//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BrownianBridge.h"
using namespace Cephei::QL::Methods::Montecarlo;
#include <gen/QL/TimeGrid.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (Cephei::QL::ITimeGrid^ timeGrid) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CTimeGrid^ _CtimeGrid;
    try
    {
#ifdef HANDLE
        _phBrownianBridge = NULL;
#endif
        _CtimeGrid = safe_cast<CTimeGrid^> (timeGrid);
        _CtimeGrid->Lock();
        QuantLib::TimeGrid& _timeGrid = static_cast<QuantLib::TimeGrid&> (_CtimeGrid->GetReference ()); 
        _ppBrownianBridge = new boost::shared_ptr<QuantLib::BrownianBridge> (new QuantLib::BrownianBridge ( _timeGrid ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CtimeGrid != nullptr) _CtimeGrid->Unlock();
    }
}
Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (Cephei::IVector<Double>^ times) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<Double>^ _Ctimes;
    try
    {
#ifdef HANDLE
        _phBrownianBridge = NULL;
#endif
        CoVector<Double>^ _Ctimes = safe_cast<CoVector<Double>^> (times);
        _Ctimes->Lock();
        INativeVector<Double>^ _NCItimes = _Ctimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCtimes = safe_cast<CDoubleVector^>(_NCItimes);
        std::vector<QuantLib::Time>& _times = static_cast<std::vector<QuantLib::Time>&> (_NCtimes->GetReference ());
        _ppBrownianBridge = new boost::shared_ptr<QuantLib::BrownianBridge> (new QuantLib::BrownianBridge ( _times ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctimes != nullptr) _Ctimes->Unlock();
    }
}
Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (UInt64 steps) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phBrownianBridge = NULL;
#endif
        QuantLib::Size _steps = (QuantLib::Size)ValueHelper::Convert (steps);
        _ppBrownianBridge = new boost::shared_ptr<QuantLib::BrownianBridge> (new QuantLib::BrownianBridge ( _steps ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (boost::shared_ptr<QuantLib::BrownianBridge>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianBridge = NULL;
#endif
	_ppBrownianBridge = &childNative;
    
}
Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (QuantLib::BrownianBridge& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBrownianBridge = NULL;
#endif
	_ppBrownianBridge = new boost::shared_ptr<QuantLib::BrownianBridge> (&childNative);
    
    _BrownianBridgeOwner = owner;
    
}

Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (CBrownianBridge^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianBridge = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBrownianBridge = new boost::shared_ptr<QuantLib::BrownianBridge> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianBridge = NULL;
#endif
	if (!t->IsSubclassOf(CBrownianBridge::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (QuantLib::Handle<QuantLib::BrownianBridge>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBrownianBridge = &childNative;
	_ppBrownianBridge = &static_cast<boost::shared_ptr<QuantLib::BrownianBridge>>(childNative.currentLink());
    
    _BrownianBridgeOwner = owner;
}
Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (QuantLib::Handle<QuantLib::BrownianBridge> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBrownianBridge = &childNative;
	_ppBrownianBridge = &static_cast<boost::shared_ptr<QuantLib::BrownianBridge>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Montecarlo::CBrownianBridge::CBrownianBridge (QuantLib::BrownianBridge childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBrownianBridge = NULL;
#endif
	_ppBrownianBridge = new boost::shared_ptr<QuantLib::BrownianBridge> (new QuantLib::BrownianBridge (childNative));
    
}
#endif

Cephei::QL::Methods::Montecarlo::CBrownianBridge::~CBrownianBridge ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBrownianBridge != NULL)
    {
	    delete _ppBrownianBridge;
        _ppBrownianBridge = NULL;
    }
}
Cephei::QL::Methods::Montecarlo::CBrownianBridge::!CBrownianBridge ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBrownianBridge != NULL)
    {
	    delete _ppBrownianBridge;
    }
}
QuantLib::BrownianBridge& Cephei::QL::Methods::Montecarlo::CBrownianBridge::GetReference ()
{
    if (_ppBrownianBridge == NULL) throw gcnew NativeNullException ();
	return **_ppBrownianBridge;
}
boost::shared_ptr<QuantLib::BrownianBridge>& Cephei::QL::Methods::Montecarlo::CBrownianBridge::GetShared ()
{
    if (_ppBrownianBridge == NULL) throw gcnew NativeNullException ();
	return *_ppBrownianBridge;
}
QuantLib::BrownianBridge* Cephei::QL::Methods::Montecarlo::CBrownianBridge::GetPointer ()
{
    if (_ppBrownianBridge == NULL) throw gcnew NativeNullException ();
	return &**_ppBrownianBridge;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BrownianBridge>& Cephei::QL::Methods::Montecarlo::CBrownianBridge::GetHandle ()
{
	if (_phBrownianBridge == NULL)
	{
		_phBrownianBridge = new Handle<QuantLib::BrownianBridge> (*_ppBrownianBridge);
	}
	return *_phBrownianBridge;
}
#endif
bool Cephei::QL::Methods::Montecarlo::CBrownianBridge::HasNative () 
{
	return (_ppBrownianBridge != NULL);
}

UInt64 Cephei::QL::Methods::Montecarlo::CBrownianBridge::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppBrownianBridge)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Methods::Montecarlo::CBrownianBridge::Times::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppBrownianBridge)->times ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Montecarlo::IBrownianBridge^ Cephei::QL::Methods::Montecarlo::CBrownianBridge_Factory::Create (Cephei::QL::ITimeGrid^ timeGrid)
{
    return gcnew CBrownianBridge ( timeGrid);
}
Cephei::QL::Methods::Montecarlo::IBrownianBridge^ Cephei::QL::Methods::Montecarlo::CBrownianBridge_Factory::Create (Cephei::IVector<Double>^ times)
{
    return gcnew CBrownianBridge ( times);
}
Cephei::QL::Methods::Montecarlo::IBrownianBridge^ Cephei::QL::Methods::Montecarlo::CBrownianBridge_Factory::Create (UInt64 steps)
{
    return gcnew CBrownianBridge ( steps);
}
